package hqb.dev.webdeploy.flow;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 停止web服务,使用该步骤请求中必须指定正确pid
 *
 * @author HQB--2024-04-22
 **/
@Slf4j
public class FlowStepStopPid extends FlowStep {

    private final Integer pid;

    public FlowStepStopPid(String name, String desc, Integer pid) {
        super(name, desc);
        this.pid = pid;
    }

    @Override
    public void execute() {
        Optional<ProcessHandle> maybePh = ProcessHandle.of(pid);
//        ProcessHandle ph = maybePh.orElseThrow(() -> new IllegalArgumentException("pid: " + pid + " 对应的进程不存在."));
        if (maybePh.isEmpty()) {
            log.info("pid: {} 对应的进程不存在.", pid);
            return;
        }
        ProcessHandle ph = maybePh.get();
        ph.destroyForcibly();
        ph.destroy();
        log.info("进程已停止.");
    }
}
